package com.privatetime.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.privatetime.common.exception.DateException;

/**
 * Help Util
 * 
 * @author cnxuzl
 * 
 */
public class PrivateTimeUtil {

	private static final String datePattern_YYYYMMDD = "yyyy-MM-dd";

	/**
	 * java.util.Date to string date
	 * 
	 * @param date
	 * @return
	 * @throws DateException
	 */
	public static String dateToStr(final Date date) throws DateException {
		if (date == null) {
			throw new DateException("Object 'date' is null...");
		}

		final SimpleDateFormat sdf = new SimpleDateFormat(datePattern_YYYYMMDD);
		final String dateStr = sdf.format(date);
		return dateStr;
	}

	/**
	 * string date to java.util.Date, the pattern is yyyy-MM-dd
	 * 
	 * @param strDate
	 * @return
	 * @throws DateException
	 */
	public static Date strToDate(final String strDate) throws DateException {
		if (strDate == null || strDate.equals("")) {
			return new Date();
		}

		final SimpleDateFormat sdf = new SimpleDateFormat(datePattern_YYYYMMDD);
		Date date = null;
		try {
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			throw new DateException("the date pattern is 'yyyy-MM-dd'");
		}
		return date;
	}

}
